//
//  TravelProductController.m
//  lv678
//
//  Created by apple on 15/6/24.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  旅游票tableview控制器

#import "TravelProductController.h"
#import "AFNetworking.h"
#import "GZTicketModel.h"
#import "GZTicketCell.h"
#import "TicketSortCollection.h"
#import "TicketDetailViewController.h"
#import "GZTicketDeatil.h"
#import "CommonLoading.h"


@interface TravelProductController ()

@property (strong, nonatomic) IBOutlet UITableView *baseTable;
@property (strong, nonatomic) NSMutableArray *group;
@property (nonatomic, strong) UIView *refreshView;
@property (nonatomic, strong) UIView *footView;
@end

@implementation TravelProductController
- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self initTableView];
}

#pragma mark - 初始化视图
- (void)initTableView{
    NSString *path                   = [[NSBundle mainBundle]pathForResource:@"UrlList" ofType:@"plist"];
    NSMutableDictionary *travel      = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
    self.url                         = [travel valueForKey:@"travelTicket"];
    
    _baseTable.delegate              = self;
    _baseTable.dataSource            = self;
    
    [self.tableView registerClass:[GZTicketCell class] forCellReuseIdentifier:@"cell"];
    
    
    //将九宫格作为tableview的header
    TicketSortCollection *headerView = [[TicketSortCollection alloc]init];
    CGRect newFrame                  = headerView.frame;
    newFrame.size.height             = newFrame.size.height +(headerView.frame.size.height)*10;
    headerView.frame                 = newFrame;
    self.tableView.tableHeaderView   = headerView;
    headerView.delegate              = self;
    
    UIColor *color = [UIColor whiteColor];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    [self.navigationController.navigationBar setTitleTextAttributes:dict];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navigation"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    
    UIView *view = [[CommonLoading initCommonLoading]refreshLoading];
    self.refreshView = view;
    self.tableView.separatorStyle = NO;
    [self.tableView addSubview:view];
    
    [self loadData];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"%f",_footView.frame.origin.y);
    _footView.frame = CGRectMake(_footView.frame.origin.x, _footView.frame.origin.y+self.tableView.contentOffset.y, _footView.frame.size.width, _footView.frame.size.height);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}
#pragma mark - TableView cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GZTicketModel *model = self.group[indexPath.row];
    //重用单元格
    static NSString *ID  = @"cell";
    GZTicketCell *cell   = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[GZTicketCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
        cell.ticket = model;
    return  cell;
}

#pragma mark - 加载数据
- (void)loadData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *tempDic = [NSDictionary dictionaryWithContentsOfFile:path];
    _url = [tempDic objectForKey:@"travelTicket"];
    AFHTTPRequestOperationManager *manager            = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer                        = [AFJSONResponseSerializer serializer];
    manager.requestSerializer                         = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSDictionary *date = @{@"serviceName":@"findAllTicket",@"queryParameters":@{}};
        [manager POST:_url parameters:date success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSArray *dateLis     = [responseObject valueForKey:@"dataList"];

            for (NSDictionary *dict in dateLis) {
            GZTicketModel *model = [GZTicketModel ticketWithDtlist:dict];
                [self.group addObject:model];
            }
//                self.tableView.separatorStyle = YES;
                [self.refreshView removeFromSuperview];
                [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self.refreshView removeFromSuperview];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"网络错误" message:@"网络连接失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }];
}

#pragma mark - 使用代理进行视图跳转
- (void)pushController:(UIViewController *)ViewController{
    [self.navigationController pushViewController:ViewController animated:YES];
}


@end
